#!/bin/bash
DATE=`date +%Y-%m-%d_%H-%M-%S`
METHOD=$1
BRANCH=$2
GROUP_LIST=$3

function IP_list(){
  if [[ ${GROUP_LIST} == "GROUP1" ]];then
     Server_IP="10.0.0.105"
     echo ${Server_IP}
     #一旦为GROUP1，要在负载均衡里下线10.0.0.105，而10.0.0.106和10.0.0.107还是在负载均衡里，然后再对10.0.0.105进行代码部署
     ssh root@10.0.0.103 ""echo disable server linux-web-80-master/10.0.0.105" | socat stdio /run/haproxy/admin.sock"
     ssh root@10.0.0.104 ""echo disable server linux-web-80-master/10.0.0.105" | socat stdio /run/haproxy/admin.sock"
  elif [[ ${GROUP_LIST} == "GROUP2" ]];then
     Server_IP="10.0.0.106 10.0.0.107"
     echo ${Server_IP}
     #要升级其他服务器，证明10.0.0.105已经升级成功，所以要把10.0.0.105再负载均衡里上线，然后就把其他服务器从负载均衡里下线，最后升级其他服务器
     ssh root@10.0.0.103 ""echo enable server linux-web-80-master/10.0.0.105" | socat stdio /run/haproxy/admin.sock"
     ssh root@10.0.0.104 ""echo enable server linux-web-80-master/10.0.0.105" | socat stdio /run/haproxy/admin.sock"
  elif [[ ${GROUP_LIST} == "GROUP3" ]];then
     Server_IP="10.0.0.105 10.0.0.106 10.0.0.107"
     echo ${Server_IP}
     #针对当前所有服务器一次性上线成功，所以不需要顺序执行
  fi
}

function clone_code(){
  cd /data/git/linux/ && rm -rf web1
  git clone -b ${BRANCH} git@10.0.0.101:linux/web1.git
  echo "代码clone完成"
}

function scanner_code(){
  cd /data/git/linux/web1 && /usr/local/sonar-scanner/bin/sonar-scanner
  echo "代码扫描完成,请打开sonarqube查看扫描结果"
}

function code_maven(){
  echo  "cd /data/git/linux/web1 && mvn clean package -Dmaven.test.skip=true"
  echo "代码编译完成"
}


function make_zip(){
  cd /data/git/linux/web1   && zip -r code.zip ./
  echo "代码打包完成"
}

function down_node(){
  for node in ${Server_IP};do
    ssh root@10.0.0.103 ""echo disable server linux-web-80-master/${node}" | socat stdio /run/haproxy/admin.sock"
    echo "${node} 从负载均衡10.0.0.103下线成功"
    ssh root@10.0.0.104 ""echo disable server linux-web-80-master/${node}" | socat stdio /run/haproxy/admin.sock"
    echo "${node} 从负载均衡10.0.0.104下线成功"
  done
}

function scp_zipfile(){
  for node in ${Server_IP};do
    scp /data/git/linux/web1/code.zip  www@${node}:/data/tomcat/tomcat_appdir/code-${DATE}.zip
    ssh www@${node} "unzip /data/tomcat/tomcat_appdir/code-${DATE}.zip  -d /data/tomcat/tomcat_webdir/code-${DATE} && rm -rf  /data/tomcat/tomcat_webapps/myapp && ln -sv  /data/tomcat/tomcat_webdir/code-${DATE} /data/tomcat/tomcat_webapps/myapp"
  done
}

function stop_tomcat(){
  for node in ${Server_IP};do
    ssh www@${node}   "/etc/init.d/tomcat stop"
  done
}

function start_tomcat(){
  for node in ${Server_IP};do
    ssh www@${node}   "/etc/init.d/tomcat start"
    #sleep 5
  done
}

function web_test(){
  sleep 5
  for node in ${Server_IP};do
    NUM=`curl -s  -I -m 10 -o /dev/null  -w %{http_code}  http://${node}:8080/myapp/index.html`
    if [[ ${NUM} -eq 200 ]];then
       echo "${node} 测试通过,即将添加到负载"
       add_node ${node}
    else
       echo "${node} 测试失败,请检查该服务器是否成功启动tomcat"
    fi
  done
}

function add_node(){
   node=$1
    echo ${node},"----->"
    #if [[ ${GROUP_LIST} == "GROUP2" ]];then
    #  ssh root@172.31.0.103 ""echo enable  server linux-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
    #  ssh root@172.31.0.104 ""echo enable  server linux-web-80-master/172.31.0.105" | socat stdio /run/haproxy/admin.sock"
    #fi
    ##########################################
    if [ ${node} == "10.0.0.105" ];then
       #如果是第一次升级，且服务器等于10.0.0.105，仅仅会执行会说明已经部署完毕
       echo "10.0.0.105 部署完毕,请进行代码测试!"
    else
       #如果是升级其他服务器，就把其他服务器添加到负载均衡里即可
      ssh root@10.0.0.103 ""echo enable  server linux-web-80-master/${node}" | socat stdio /run/haproxy/admin.sock"
      ssh root@10.0.0.104 ""echo enable  server linux-web-80-master/${node}" | socat stdio /run/haproxy/admin.sock"
    fi
}

function rollback_last_version(){
  for node in ${Server_IP};do
   echo $node
   NOW_VERSION=`ssh www@${node} ""/bin/ls -l  -rt /data/tomcat/tomcat_webapps/ | awk -F"->" '{print $2}'  | tail -n1""`
   NOW_VERSION=`basename ${NOW_VERSION}`
   echo $NOW_VERSION,"NOW_VERSION"
   #NAME为上一个版本的名称
   NAME=`ssh  www@${node}  ""ls  -l  -rt  /data/tomcat/tomcat_webdir/ | grep -B 1 ${NOW_VERSION} | head -n1 | awk '{print $9}'""`
   echo $NAME,"NAME"
   ssh www@${node} "rm -rf /data/tomcat/tomcat_webapps/myapp && ln -sv  /data/tomcat/tomcat_webdir/${NAME} /data/tomcat/tomcat_webapps/myapp"
  done
}

function delete_history_version(){
  for node in ${Server_IP};do
    ssh www@${node} "rm -rf /data/tomcat/tomcat_appdir/*"
    NUM=`ssh www@${node}  ""/bin/ls -l -d   -rt /data/tomcat/tomcat_webdir/code-* | wc -l""`
    echo $NUM
      if [ ${NUM} -gt 10 ];then
         NAME=`ssh www@${node} ""/bin/ls -l -d   -rt /data/tomcat/tomcat_webdir/code-* | head -n1 | awk '{print $9}'""`
         ssh www@${node} "rm -rf ${NAME}"
        echo "${node} 删除历史版本 /data/tomcat/tomcat_webdir/${NAME}成功!"
      fi
  done
}

main(){
   case $1  in
      deploy)
        IP_list;
        clone_code;
        scanner_code;
        make_zip;
        down_node;
        stop_tomcat;
        scp_zipfile;
        start_tomcat;
        web_test;
        delete_history_version;
         ;;
      rollback_last_version)
        IP_list;
        #echo ${Server_IP}
        down_node;
        stop_tomcat;
        rollback_last_version;
        start_tomcat;
        web_test;
         ;;
    esac
}

main $1 $2 $3